import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/company_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';

class AccountManageController extends GetxController {
  MultiState multiState = MultiState.loading;
  ScrollController scrollController = ScrollController();
  List<CompanyEntity> companyList = [];
  String paymentAccountId = "";

  @override
  void onReady() {
    super.onReady();
    paymentAccountId = Get.arguments ?? "";
    getCompanyList();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getCompanyList();
  }

  Future<void> getCompanyList() async {
    httpManager.post(
      Get.context,
      HttpAddress.GET_COMPANY_LIST_SETTLEMENT,
      {},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          multiState = MultiState.fail;
          update();
          return;
        }
        if (data is List) {
          companyList = data.map((element) {
            return CompanyEntity().fromJson((element as Map<String, dynamic>));
          }).toList();
          if (companyList.isNotEmpty) {
            companyList.removeWhere((element) {
              return element.id == paymentAccountId;
            });
          }
          multiState = companyList.isNotEmpty ? MultiState.success : MultiState.empty;
          update();
        }
      },
      onFail: (code, msg) {
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        multiState = MultiState.error;
        update();
      },
    );
  }

  Future<void> searchAccount() async {
    if (companyList.isNotEmpty) {
      dynamic data = await Get.toNamed(Routes.searchAccountManage, arguments: {
        Constant.PARAM_COMPANY_ID: paymentAccountId,
        Constant.PARAM_COMPANY_LIST: companyList,
      });
      if (data != null && data is Map) {
        Get.back(result: {
          Constant.PARAM_COMPANY_ID: data[Constant.PARAM_COMPANY_ID],
          Constant.PARAM_COMPANY_NAME: data[Constant.PARAM_COMPANY_NAME],
          Constant.PARAM_COMPANY_CODE: data[Constant.PARAM_COMPANY_CODE],
        });
      }
    }
  }

  Future<void> addAccount() async {
    dynamic data = await Get.toNamed(Routes.addAccount, arguments: paymentAccountId);
    if (data != null && data is Map) {
      CompanyEntity companyEntity = CompanyEntity();
      companyEntity.id = data[Constant.PARAM_COMPANY_ID];
      companyEntity.name = data[Constant.PARAM_COMPANY_NAME];
      companyEntity.code = data[Constant.PARAM_COMPANY_CODE];
      companyList.insert(0, companyEntity);
      multiState = MultiState.success;
      if (scrollController.hasClients) {
        scrollController.jumpTo(0);
      }
      update();
    }
  }

  void setSettlementState(String companyId, bool isNotSettlement) {
    httpManager.post(
      Get.context,
      HttpAddress.SET_SETTLEMENT_STATE,
      {
        "superId": paymentAccountId,
        "id": companyId,
        "status": isNotSettlement ? 0 : 1, //不再结算:1;结算:0
      },
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        updateSettlementState(companyId, !isNotSettlement);
      },
    );
  }

  void updateSettlementState(String companyId, bool isNotSettlement) {
    int position = -1;
    for (int i = 0; i < companyList.length; i++) {
      String id = companyList[i].id ?? "";
      if (companyId == id) {
        position = i;
        break;
      }
    }
    if (position != -1) {
      CompanyEntity companyEntity = companyList[position];
      companyEntity.isNotSettlement = isNotSettlement;
      update();
    }
  }

  void backResult(String companyId, String companyName, String companyCode) {
    if (companyId.isEmpty) {
      ToastUtils.toast("公司id为空");
      return;
    }
    Get.back(result: {
      Constant.PARAM_COMPANY_ID: companyId,
      Constant.PARAM_COMPANY_NAME: companyName,
      Constant.PARAM_COMPANY_CODE: companyCode,
    });
  }
}
